package com.xjscrm.console.service.customer.operatePlan.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.*;
import com.xjscrm.common.enums.RedisCacheKeyEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.common.vo.grouppull.GroupPullData;
import com.xjscrm.common.vo.grouppull.GroupRule;
import com.xjscrm.common.vo.grouppull.GroupRuleInfo;
import com.xjscrm.console.mapper.*;
import com.xjscrm.console.param.operatePlan.*;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.customer.operatePlan.FaqReplyService;
import com.xjscrm.console.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FaqReplyServiceImpl implements FaqReplyService {

    @Autowired
    private FaqReplyMapper faqReplyMapper;

    @Autowired
    private FaqReplyQuestionMapper faqReplyQuestionMapper;

    @Autowired
    private FaqReplyDeviceMapper faqReplyDeviceMapper;

    @Autowired
    private WwDeviceMapper wwDeviceMapper;

    @Autowired
    private DeviceGroupRelationMapper deviceGroupRelationMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFaqReply(FaqReplyDto faqReplyDto) {
        //验证如参数
        faqReplyParaVerify(faqReplyDto);

        //新增智能应答方案
        FaqReply faqReply = new FaqReply();
        BeanUtil.copyProperties(faqReplyDto,faqReply);
        int time = DateUtil.getTime();
        Long sixDigitsNum = getSixDigitsNum();
        faqReply.setId(sixDigitsNum);
        faqReply.setCreateTime(time);
        faqReply.setUpdateTime(time);
        faqReplyMapper.insertFaqReply(faqReply);

        //关键词应答，添加问题
        if(faqReplyDto.getKeywordSwitch()!=null&&faqReplyDto.getKeywordSwitch()==1){
            for (FaqReplyQuestionDto faqReplyQuestionDto:faqReplyDto.getFaqReplyQuestionDtoList()) {
                FaqReplyQuestion faqReplyQuestion=new FaqReplyQuestion();
                BeanUtil.copyProperties(faqReplyQuestionDto,faqReplyQuestion);
                faqReplyQuestion.setReplyId(faqReply.getId());
                faqReplyQuestion.setMerId(faqReply.getMerId());
                faqReplyQuestionMapper.addFaqReplyQuestion(faqReplyQuestion);
            }
        }

        //添加应用机器人
        if(faqReplyDto.getDeviceUniqueIds()!=null&&!faqReplyDto.getDeviceUniqueIds().isEmpty()){
            for (Long deviceUniqueId:faqReplyDto.getDeviceUniqueIds()) {
                FaqReplyDevice faqReplyDevice=new FaqReplyDevice();
                faqReplyDevice.setTaskId(faqReply.getId());
                faqReplyDevice.setDeviceUniqueId(deviceUniqueId);
                faqReplyDevice.setMerId(faqReply.getMerId());
                faqReplyDeviceMapper.addFaqReplyDevice(faqReplyDevice);
            }
        }
    }

    @Override
    public void updateFaqReplyName(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        if(faqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(StringUtils.isBlank(faqReplyDto.getName())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        FaqReply faqReply= faqReplyMapper.getById(faqReplyDto.getId(),faqReplyDto.getMerId());
        if(faqReply==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该智能应答方案不存在");
        }
        faqReply.setName(faqReplyDto.getName());
        faqReplyMapper.updateFaqReply(faqReply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFaqReply(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        //验证如参数
        faqReplyParaVerify(faqReplyDto);

        FaqReply faqReply= faqReplyMapper.getById(faqReplyDto.getId(),faqReplyDto.getMerId());
        if(faqReply==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该智能应答方案不存在");
        }
        //修改方案
        BeanUtil.copyProperties(faqReplyDto,faqReply);
        faqReplyMapper.update(faqReply);



        if(faqReplyDto.getKeywordSwitch()!=null&&faqReplyDto.getKeywordSwitch()==1){
            //删除关键词应答的问题
            FaqReplyQuestionQuery faqReplyQuestionQuery = new FaqReplyQuestionQuery();
            faqReplyQuestionQuery.setMerId(faqReply.getMerId());
            faqReplyQuestionQuery.setReplyId(faqReply.getId());
            List<FaqReplyQuestionVo> faqReplyQuestionVoList=faqReplyQuestionMapper.getFaqReplyQuestionList(faqReplyQuestionQuery);
            if(faqReplyQuestionVoList!=null&&!faqReplyQuestionVoList.isEmpty()){
                faqReplyQuestionVoList.forEach(it->faqReplyQuestionMapper.deleById(it.getId(),it.getMerId()));
            }

            //关键词应答，添加新问题
            for (FaqReplyQuestionDto faqReplyQuestionDto:faqReplyDto.getFaqReplyQuestionDtoList()) {
                FaqReplyQuestion faqReplyQuestion=new FaqReplyQuestion();
                BeanUtil.copyProperties(faqReplyQuestionDto,faqReplyQuestion);
                faqReplyQuestion.setReplyId(faqReply.getId());
                faqReplyQuestion.setMerId(faqReply.getMerId());
                faqReplyQuestionMapper.addFaqReplyQuestion(faqReplyQuestion);
            }
        }

//
//        //删除和机器人的关系
//        FaqReplyDeviceQuery faqReplyDeviceQuery=new FaqReplyDeviceQuery();
//        faqReplyDeviceQuery.setMerId(faqReply.getMerId());
//        faqReplyDeviceQuery.setTaskId(faqReply.getId());
//        List<FaqReplyDeviceVo> faqReplyDeviceVoList=faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);
//        if(faqReplyDeviceVoList!=null&&!faqReplyDeviceVoList.isEmpty()){
//            faqReplyDeviceVoList.forEach(it->faqReplyDeviceMapper.deleById(it.getId(),it.getMerId()));
//        }
//
//


//
//
//        //添加应用机器人
//        if(faqReplyDto.getDeviceUniqueIds()!=null&&!faqReplyDto.getDeviceUniqueIds().isEmpty()){
//            for (Long deviceUniqueId:faqReplyDto.getDeviceUniqueIds()) {
//                FaqReplyDevice faqReplyDevice=new FaqReplyDevice();
//                faqReplyDevice.setTaskId(faqReply.getId());
//                faqReplyDevice.setDeviceUniqueId(deviceUniqueId);
//                faqReplyDevice.setMerId(faqReply.getMerId());
//                faqReplyDeviceMapper.addFaqReplyDevice(faqReplyDevice);
//            }
//        }
    }

    @Override
    public Object getFaqReplyList(FaqReplyQuery faqReplyQuery) {
        if(faqReplyQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        String orderBy = OrderByUtil.getOrderBy(faqReplyQuery.getOrderByField(), faqReplyQuery.isAsc());
        PageHelper.startPage(faqReplyQuery.getCurrent(),faqReplyQuery.getSize(),orderBy);
        List<FaqReplyVo> faqReplyVoList=faqReplyMapper.getFaqReplyList(faqReplyQuery);
        if(faqReplyVoList!=null&&!faqReplyVoList.isEmpty()){
            for (FaqReplyVo faqReplyVo:faqReplyVoList) {
                try {
                    FaqReplyDeviceQuery faqReplyDeviceQuery=new FaqReplyDeviceQuery();
                    faqReplyDeviceQuery.setMerId(faqReplyVo.getMerId());
                    faqReplyDeviceQuery.setTaskId(faqReplyVo.getId());
                    List<FaqReplyDeviceVo> faqReplyDeviceList = faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);
                    if(faqReplyDeviceList!=null&&!faqReplyDeviceList.isEmpty()){
                        List<Long> collect = faqReplyDeviceList.stream().map(FaqReplyDeviceVo::getDeviceUniqueId).distinct().collect(Collectors.toList());
                        faqReplyVo.setUsedDeviceUniqueIds(collect);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        return new MyPageInfo<>(faqReplyVoList);
    }

    @Override
    public Object getFaqReplyDetail(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(faqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        FaqReply faqReply = faqReplyMapper.getById(faqReplyDto.getId(), faqReplyDto.getMerId());
        FaqReplyVo faqReplyVo = new FaqReplyVo();
        BeanUtil.copyProperties(faqReply,faqReplyVo);

        //如果是关键词应答，放入问题
            List<FaqReplyQuestionVo> faqReplyQuestionVoList=new ArrayList<>();
            FaqReplyQuestionQuery faqReplyQuestionQuery=new FaqReplyQuestionQuery();
            faqReplyQuestionQuery.setReplyId(faqReply.getId());
            faqReplyQuestionQuery.setMerId(faqReply.getMerId());
            List<FaqReplyQuestionVo> faqReplyQuestionList = faqReplyQuestionMapper.getFaqReplyQuestionList(faqReplyQuestionQuery);
            if(faqReplyQuestionList!=null&&!faqReplyQuestionList.isEmpty()){
                faqReplyQuestionList.forEach(faqReplyQuestionVoList::add);
            }
            faqReplyVo.setFaqReplyQuestionDtoList(faqReplyQuestionVoList);


        FaqReplyDeviceQuery faqReplyDeviceQuery=new FaqReplyDeviceQuery();
        faqReplyDeviceQuery.setMerId(faqReplyVo.getMerId());
        faqReplyDeviceQuery.setTaskId(faqReplyVo.getId());
        List<FaqReplyDeviceVo> faqReplyDeviceList = faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);
        if(faqReplyDeviceList!=null&&!faqReplyDeviceList.isEmpty()){
            List<Long> collect = faqReplyDeviceList.stream().map(FaqReplyDeviceVo::getDeviceUniqueId).distinct().collect(Collectors.toList());
            faqReplyVo.setUsedDeviceUniqueIds(collect);
        }

        return faqReplyVo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleFaqReply(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(faqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        FaqReply faqReply = faqReplyMapper.getById(faqReplyDto.getId(), faqReplyDto.getMerId());

        //删除关键词应答的问题
        FaqReplyQuestionQuery faqReplyQuestionQuery = new FaqReplyQuestionQuery();
        faqReplyQuestionQuery.setMerId(faqReply.getMerId());
        faqReplyQuestionQuery.setReplyId(faqReply.getId());
        List<FaqReplyQuestionVo> faqReplyQuestionVoList=faqReplyQuestionMapper.getFaqReplyQuestionList(faqReplyQuestionQuery);
        if(faqReplyQuestionVoList!=null&&!faqReplyQuestionVoList.isEmpty()){
            faqReplyQuestionVoList.forEach(it->faqReplyQuestionMapper.deleById(it.getId(),it.getMerId()));
        }

        //删除和机器人的关系
        FaqReplyDeviceQuery faqReplyDeviceQuery=new FaqReplyDeviceQuery();
        faqReplyDeviceQuery.setMerId(faqReply.getMerId());
        faqReplyDeviceQuery.setTaskId(faqReply.getId());
        List<FaqReplyDeviceVo> faqReplyDeviceVoList=faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);
        if(faqReplyDeviceVoList!=null&&!faqReplyDeviceVoList.isEmpty()){
            faqReplyDeviceVoList.forEach(it->faqReplyDeviceMapper.deleById(it.getId(),it.getMerId()));
        }

        //删除方案
        faqReplyMapper.deleById(faqReply.getId(),faqReply.getMerId());

    }


    @Override
    public Object getFaqReplyDeviceList(FaqReplyDeviceQuery faqReplyDeviceQuery) {
        if(faqReplyDeviceQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(faqReplyDeviceQuery.getTaskId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        List<Long> duviceUniqueId=new ArrayList<>();

        //关键词中有机器人ID或者机器人的name
        List<Long> getDeviceUniqueIds=new ArrayList<>();
        if(!StringUtils.isBlank(faqReplyDeviceQuery.getKeyword())){
            //机器人ID
            WwDeviceQuery wwDeviceQuery=new WwDeviceQuery();
            wwDeviceQuery.setMerId(faqReplyDeviceQuery.getMerId());
            wwDeviceQuery.setKeyword(faqReplyDeviceQuery.getKeyword());
            List<WwDeviceVo> wwDeviceList = wwDeviceMapper.getWwDeviceList(wwDeviceQuery);
            if(wwDeviceList!=null&&!wwDeviceList.isEmpty()){
                wwDeviceList.forEach(it->getDeviceUniqueIds.add(it.getId()));
            }
            if(getDeviceUniqueIds.isEmpty()){
                return  new MyPageInfo<>(null);
            }
        }

       //分组，获取分组中的机器人ID
        List<Long> groupDeviceUniqueIds=new ArrayList<>();
        if(faqReplyDeviceQuery.getGroupId()!=null){
            DeviceGroupRelationQuery deviceGroupRelationQuery=new DeviceGroupRelationQuery();
            deviceGroupRelationQuery.setMerId(faqReplyDeviceQuery.getMerId());
            deviceGroupRelationQuery.setGroupId(faqReplyDeviceQuery.getGroupId());
            List<DeviceGroupRelation> deviceGroupRelationList = deviceGroupRelationMapper.deviceGroupRelationList(deviceGroupRelationQuery);
            if(deviceGroupRelationList!=null&&!deviceGroupRelationList.isEmpty()){
                groupDeviceUniqueIds = deviceGroupRelationList.stream().map(DeviceGroupRelation::getDeviceUniqueId).distinct().collect(Collectors.toList());
            }
            if(groupDeviceUniqueIds.isEmpty()){
                return new MyPageInfo<>(null);
            }
        }

        if(!getDeviceUniqueIds.isEmpty()&&groupDeviceUniqueIds.isEmpty()){
            duviceUniqueId=getDeviceUniqueIds;
        }
        if(getDeviceUniqueIds.isEmpty()&&!groupDeviceUniqueIds.isEmpty()){
            duviceUniqueId=groupDeviceUniqueIds;
        }
        if(!getDeviceUniqueIds.isEmpty()&&!groupDeviceUniqueIds.isEmpty()){
            //获取交集
            duviceUniqueId = getDeviceUniqueIds.stream().filter(groupDeviceUniqueIds::contains).collect(Collectors.toList());
        }

        if(!duviceUniqueId.isEmpty()){
            faqReplyDeviceQuery.setDeviceUniqueIds(duviceUniqueId);
        }


        String orderBy = OrderByUtil.getOrderBy(faqReplyDeviceQuery.getOrderByField(), faqReplyDeviceQuery.isAsc());
        PageHelper.startPage(faqReplyDeviceQuery.getCurrent(),faqReplyDeviceQuery.getSize(),orderBy);
        List<FaqReplyDeviceVo> faqReplyDeviceList = faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);
        if(faqReplyDeviceList!=null&&!faqReplyDeviceList.isEmpty()){
            for (FaqReplyDeviceVo faqReplyDeviceVo:faqReplyDeviceList) {
                try {
                    WwDevice wwDevice = wwDeviceMapper.getById(faqReplyDeviceVo.getDeviceUniqueId(), faqReplyDeviceVo.getMerId());
                    if(wwDevice!=null){
                        faqReplyDeviceVo.setDeviceName(wwDevice.getDeviceName());
                        faqReplyDeviceVo.setHeadImgUrl(wwDevice.getHeadImgUrl());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }


        return new MyPageInfo<>(faqReplyDeviceList);
    }

    @Override
    public void deleFaqReplyDevice(BatchFaqReplyDto batchFaqReplyDto) {
        if(batchFaqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(batchFaqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        faqReplyDeviceMapper.deleById(batchFaqReplyDto.getId(), batchFaqReplyDto.getMerId());
    }


    @Override
    public void AddReplyDevice(BatchFaqReplyDto batchFaqReplyDto) {
        if(batchFaqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(batchFaqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        if(batchFaqReplyDto.getDeviceUniqueIds()==null||batchFaqReplyDto.getDeviceUniqueIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long deviceUniqueId:batchFaqReplyDto.getDeviceUniqueIds()) {
           try {
               FaqReplyDevice faqReplyDevice=new FaqReplyDevice();
               faqReplyDevice.setMerId(batchFaqReplyDto.getMerId());
               faqReplyDevice.setTaskId(batchFaqReplyDto.getId());
               faqReplyDevice.setDeviceUniqueId(deviceUniqueId);
               faqReplyDeviceMapper.addFaqReplyDevice(faqReplyDevice);
           }catch (Exception e){
               e.printStackTrace();
           }
        }

    }

    private void faqReplyParaVerify(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(StringUtils.isBlank(faqReplyDto.getName())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(faqReplyDto.getUseType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        //指定日期范围，日期范围不能为空
        if(faqReplyDto.getUseType()==1){
          if(StringUtils.isBlank(faqReplyDto.getUseDayStart())||StringUtils.isBlank(faqReplyDto.getUseDayEnd())){
              throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "选择指定日期，日期不能为空");
          }
        }

        if(faqReplyDto.getTimeStart()==null|| faqReplyDto.getTimeEnd()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "应答时段不能为空");
        }

//        if(faqReplyDto.getRateMin()==null|| faqReplyDto.getRateMax()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "应答延时不能为空");
//        }

        //应答对象 客户
        if(faqReplyDto.getCustomerSwitch()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(faqReplyDto.getCustomerSwitch()==1){
           if(faqReplyDto.getCustomerType()==null){
               throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "客户应答开关开启，客户类型不能为空");
           }
           if(faqReplyDto.getCustomerType()==1){
               if(StringUtils.isBlank(faqReplyDto.getCustomerEnterpriseTags())&& faqReplyDto.getCustomerRemarkOpt()==null&&faqReplyDto.getCustomerEnterpriseTagOpt()==null&& faqReplyDto.getCustomerGenders()==null&&(faqReplyDto.getCustomerCreateDayStart()==null|| faqReplyDto.getCustomerCreateDayEnd()==null)){
                   throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "客户应答开关开启,部分客户，下面选项必填一个");
               }
           }
        }

        //应答对象 客户群
        if(faqReplyDto.getGroupSwitch()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(faqReplyDto.getGroupSwitch()==1){
            if(faqReplyDto.getGroupType()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "应答群开关开启，群类型不能为空");
            }
            if(faqReplyDto.getGroupType()==1){
                if(faqReplyDto.getGroupNameKeywordOpt()==null&& faqReplyDto.getGroupTagOpt()==null&& faqReplyDto.getGroupMemberIdentitys()==null&&(faqReplyDto.getGroupMemberTotalMin()==null|| faqReplyDto.getGroupMemberTotalMax()==null)&&(faqReplyDto.getGroupCreateDayStart()==null|| faqReplyDto.getGroupCreateDayEnd()==null)){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "客户应答开关开启,部分群，下面选项必填一个");
                }
            }
        }

//        if(faqReplyDto.getExcludeRobot()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }
//
//        if(faqReplyDto.getExcludeRobot()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }

        //关键词应答
//        if (faqReplyDto.getKeywordSwitch()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }
        if(faqReplyDto.getKeywordSwitch()!=null&&faqReplyDto.getKeywordSwitch()==1){
            if(StringUtils.isBlank(faqReplyDto.getKeywordGuide())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
            if(faqReplyDto.getFaqReplyQuestionDtoList()==null|| faqReplyDto.getFaqReplyQuestionDtoList().isEmpty()){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
            }
        }

        //图片应答
//        if(faqReplyDto.getImgSwitch()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }
        if(faqReplyDto.getImgSwitch()!=null&&faqReplyDto.getImgSwitch()==1){
            if(StringUtils.isBlank(faqReplyDto.getImgMsgDatas())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "图片应答开启，图片应答消息不能为空");
            }
        }

        //默认应答开关
//        if(faqReplyDto.getDefaultSwitch()==null){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }
        if(faqReplyDto.getDefaultSwitch()!=null&&faqReplyDto.getDefaultSwitch()==1){
            if(faqReplyDto.getDefaultReplyDelayTime()==null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "默认应答开关开启，需设置延迟时间");
            }
            if(StringUtils.isBlank(faqReplyDto.getDefaultMsgDatas())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "默认应答开关开启，默认应答消息列表不能为空");
            }
        }
    }


    @Override
    public Object getUsedDeviceUniqueIds(FaqReplyDto faqReplyDto) {
        if(faqReplyDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(faqReplyDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        FaqReply faqReply = faqReplyMapper.getById(faqReplyDto.getId(), faqReplyDto.getMerId());
        if(faqReply==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案不存在");
        }
        FaqReplyDeviceQuery faqReplyDeviceQuery=new FaqReplyDeviceQuery();
        faqReplyDeviceQuery.setMerId(faqReply.getMerId());
        faqReplyDeviceQuery.setTaskId(faqReply.getId());
        List<FaqReplyDeviceVo> faqReplyDeviceList = faqReplyDeviceMapper.getFaqReplyDeviceList(faqReplyDeviceQuery);

        if(faqReplyDeviceList!=null&&!faqReplyDeviceList.isEmpty()){
            List<Long> collect = faqReplyDeviceList.stream().map(FaqReplyDeviceVo::getDeviceUniqueId).distinct().collect(Collectors.toList());
            return collect;
        }else {
            return null;
        }

    }


    @Override
    public Object findAddFangetEffectuateGroupGroup(EffectuateGroupDto effectuateGroupDto) {
        if(effectuateGroupDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(effectuateGroupDto.getPlanId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(StringUtils.isBlank(effectuateGroupDto.getGroupPullKey())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        FaqReply faqReply = faqReplyMapper.getById(effectuateGroupDto.getPlanId(), effectuateGroupDto.getMerId());

        if(faqReply==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案不存在");
        }
        if(StringUtils.isBlank(faqReply.getKeywordGuide())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案引导语不存在");
        }
        List<GroupPullData> groupPullDataList = JSONObject.parseArray(faqReply.getKeywordGuide(), GroupPullData.class);
        if(groupPullDataList==null||groupPullDataList.isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该方案引导语不存在");
        }
        List<EffectuateGroupVo> effectuateGroupVoList=new ArrayList<>();

        for (GroupPullData groupPullData:groupPullDataList) {
            if(groupPullData.getMsgType()!=null&&groupPullData.getMsgType().equals(100)){
                if(!StringUtils.isBlank(groupPullData.getGroupPullKey())&&groupPullData.getGroupPullKey().equals(effectuateGroupDto.getGroupPullKey())){

                    if(groupPullData.getGroupRules()!=null&&!groupPullData.getGroupRules().isEmpty()){
                        effectuateGroupVoList=new ArrayList<>();
                        for (GroupRule groupRule:groupPullData.getGroupRules()) {
                            EffectuateGroupVo effectuateGroupVo = new EffectuateGroupVo();
                            effectuateGroupVo.setDeviceId(groupRule.getDeviceId());
                            effectuateGroupVo.setDeviceUniqueId(groupRule.getDeviceUniqueId());
                            effectuateGroupVo.setDeviceName(groupRule.getDeviceName());
                            effectuateGroupVo.setDeviceHeadImageUrl(groupRule.getDeviceHeadImageUrl());
                            if(groupRule.getGroupRuleInfos()!=null&&!groupRule.getGroupRuleInfos().isEmpty()){
                                List<GroupRuleInfoVo> groupRuleInfoVoList=new ArrayList<>();
                                for (GroupRuleInfo groupRuleInfo:groupRule.getGroupRuleInfos()) {
                                    GroupRuleInfoVo groupRuleInfoVo = new GroupRuleInfoVo();
                                    groupRuleInfoVo.setDeviceId(groupRule.getDeviceId());
                                    groupRuleInfoVo.setDeviceUniqueId(groupRule.getDeviceUniqueId());
                                    groupRuleInfoVo.setDeviceName(groupRule.getDeviceName());
                                    groupRuleInfoVo.setGroupId(groupRuleInfo.getGroupId());
                                    groupRuleInfoVo.setGroupWxid(groupRuleInfo.getGroupWxid());
                                    groupRuleInfoVo.setGroupNick(groupRuleInfo.getGroupNick());
                                    groupRuleInfoVo.setGenders(groupRuleInfo.getGenders());
                                    groupRuleInfoVo.setDeviceHeadImageUrl(groupRule.getDeviceHeadImageUrl());
                                    groupRuleInfoVo.setGroupMemberNum((Integer) redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.GROUP_MEMBER_COUNT.getKey(),groupRuleInfo.getGroupWxid())));
                                    groupRuleInfoVoList.add(groupRuleInfoVo);
                                }
                                effectuateGroupVo.setGroupRuleInfoVoList(groupRuleInfoVoList);
                            }
                            effectuateGroupVoList.add(effectuateGroupVo);
                        }

                    }
                }
            }
        }

        return effectuateGroupVoList;
    }

    private Long getSixDigitsNum(){
        return  (long) ((Math.random() * 9 + 1) * 100000);
    }
}
